package com.example.test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.provider.CallLog.Calls;

/**
 * 获取通话记录
 * @author yzhao
 *
 */
public class PhoneSource {
	
	public static void test(Context context) {
		List<PhoneInfo> list = new ArrayList<PhoneInfo>();  
		Cursor cursor = context.getContentResolver().query(
				CallLog.Calls.CONTENT_URI, null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				PhoneInfo calls = new PhoneInfo();
				// 号码
				String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
				// 呼叫类型
				String type;
				switch (Integer.parseInt(cursor.getString(cursor.getColumnIndex(Calls.TYPE)))) {
				case Calls.INCOMING_TYPE:
					type = "呼入";
					break;
				case Calls.OUTGOING_TYPE:
					type = "呼出";
					break;
				case Calls.MISSED_TYPE:
					type = "未接";
					break;
				default:
					type = "挂断";// 应该是挂断.根据我手机类型判断出的
					break;
				}
				SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(Calls.DATE))));
				// 呼叫时间
				String time = sfd.format(date);
				// 联系人
				String name = cursor.getString(cursor.getColumnIndexOrThrow(Calls.CACHED_NAME));
				// 通话时间,单位:s
				String duration = cursor.getString(cursor.getColumnIndexOrThrow(Calls.DURATION));
				calls.setName(name);
				calls.setPhoneDate(duration);
				calls.setSendTime(time);
				calls.setType(type);
				calls.setPhoneNumber(number);
				list.add(calls);

			} while (cursor.moveToNext());
			list.size();
		}
	}
}
